﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Jlob.OpenErpNet;
using Jlob.OpenErpNet.Entities;
using System.Linq;
using System.Collections.Generic;

namespace Jlob.OpenErpNet.Test
{
    [TestClass]
    public class ModelTest
    {
        private const string OpenErpServer = "http://192.168.230.128:8069";

        #region Employee
        
        [TestMethod]
        public void Test_Create_Employee_with_min_values()
        {
            Employee em = new Employee();
            em.Name = "Test";
            OpenErpService openErpService = new OpenErpService(OpenErpServer, "POLTEST", "admin", "admin");
            openErpService.AddEntity<Employee>(em);
            em = openErpService.GetEntity<Employee>(e => e.Name.Equals("Test"));
            Assert.IsNotNull(em);
        }

        [TestMethod]
        public void Test_Update_Employee()
        {
            Employee em = new Employee();
            OpenErpService openErpService = new OpenErpService(OpenErpServer, "POLTEST", "admin", "admin");
            em = openErpService.GetEntity<Employee>(e => e.Name.Equals("Test"));
            em.IdentificationNumber = "45685424K";
            openErpService.UpdateEntity<Employee>(em);
            em = openErpService.GetEntity<Employee>(e => e.IdentificationNumber.Equals("45685424K"));
            Assert.IsNotNull(em);
        }

        [TestMethod]
        public void Test_Delete_Employee()
        {
            Employee em = new Employee();
            OpenErpService openErpService = new OpenErpService(OpenErpServer, "POLTEST", "admin", "admin");
            em = openErpService.GetEntity<Employee>(e => e.Name.Equals("Test"));
            openErpService.DeleteEntity<Employee>(em);
            em = openErpService.GetEntity<Employee>(e => e.Name.Equals("Test"));
            Assert.IsNull(em);
        }

        [TestMethod]
        public void Test_Include_User_in_employee()
        {
            IEnumerable<Employee> employees;
            OpenErpService openErpService = new OpenErpService(OpenErpServer, "POLTEST", "admin", "admin");
            employees = openErpService.GetEntities<Employee>(e => e.Name.Equals("Administrator")).Include(e => e.User);
            Assert.IsNotNull(employees.First().User);
        }

        #endregion
    }
}
